package com.platform.sys.modular.syncdata.controller;


import com.platform.sys.modular.syncdata.entity.SyncDataSource;
import com.platform.sys.modular.syncdata.enums.SyncDataSourceExceptionEnum;
import com.platform.sys.modular.syncdata.param.SyncDataSourceParam;
import com.platform.sys.modular.syncdata.service.SyncDataSourceService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import com.platform.core.annotion.Permission;
import com.platform.core.pojo.page.PageResult;
import com.platform.core.pojo.response.ResponseData;
import com.platform.core.pojo.response.SuccessResponseData;

import javax.annotation.Resource;
import java.util.List;

/**
 * 要操作其他库的元数据字段配置控制器
 *
 * @author gyp
 * @date 2022-12-04 20:43:04
 */
@RestController
public class SyncDataSourceController {

    @Resource
    private SyncDataSourceService syncDataSourceService;

    /**
     * 查询要操作其他库的元数据字段配置
     *
     * @author gyp
     * @date 2022-12-04 20:43:04
     */
    @Permission
    @GetMapping("/syncDataSource/page")
    public ResponseData<PageResult<SyncDataSource>> page(SyncDataSourceParam syncDataSourceParam) {
        PageResult<SyncDataSource> page = syncDataSourceService.page(syncDataSourceParam);
        return SuccessResponseData.success(page);
    }

    /**
     * 添加要操作其他库的元数据字段配置
     *
     * @author gyp
     * @date 2022-12-04 20:43:04
     */
    @Permission
    @PostMapping("/syncDataSource/add")
    public ResponseData add(@RequestBody @Validated(SyncDataSourceParam.add.class) SyncDataSourceParam syncDataSourceParam) {

        /* 1、添加时对标识字段判重 */
        List<SyncDataSource> list = syncDataSourceService.list(syncDataSourceParam);
        if(list.size() > 0){
            return SuccessResponseData.error(SyncDataSourceExceptionEnum.CODE_REPEAT.getCode(),
                    SyncDataSourceExceptionEnum.CODE_REPEAT.getMessage());
        }
        syncDataSourceService.add(syncDataSourceParam);
        return SuccessResponseData.success();
    }

    /**
     * 删除要操作其他库的元数据字段配置，可批量删除
     *
     * @author gyp
     * @date 2022-12-04 20:43:04
     */
    @Permission
    @PostMapping("/syncDataSource/delete")
    public ResponseData delete(@RequestBody @Validated(SyncDataSourceParam.delete.class) List<SyncDataSourceParam> syncDataSourceParamList) {
        syncDataSourceService.delete(syncDataSourceParamList);
        return SuccessResponseData.success();
    }

    /**
     * 编辑要操作其他库的元数据字段配置
     *
     * @author gyp
     * @date 2022-12-04 20:43:04
     */
    @Permission
    @PostMapping("/syncDataSource/edit")
    public ResponseData edit(@RequestBody @Validated(SyncDataSourceParam.edit.class) SyncDataSourceParam syncDataSourceParam) {
        syncDataSourceService.edit(syncDataSourceParam);
        return SuccessResponseData.success();
    }

    /**
     * 查看要操作其他库的元数据字段配置
     *
     * @author gyp
     * @date 2022-12-04 20:43:04
     */
    @Permission
    @PostMapping("/syncDataSource/detail")
    public ResponseData<SyncDataSource> detail(@Validated(SyncDataSourceParam.detail.class) SyncDataSourceParam syncDataSourceParam) {
        return SuccessResponseData.success(syncDataSourceService.detail(syncDataSourceParam));
    }

    /**
     * 要操作其他库的元数据字段配置列表
     *
     * @author gyp
     * @date 2022-12-04 20:43:04
     */
    @Permission
    @PostMapping("/syncDataSource/list")
    public ResponseData<List<SyncDataSource>> list(SyncDataSourceParam syncDataSourceParam) {
        return SuccessResponseData.success(syncDataSourceService.list(syncDataSourceParam));
    }

    /**
     * 导出系统用户
     *
     * @author gyp
     * @date 2022-12-04 20:43:04
     */
    @Permission
    @PostMapping("/syncDataSource/export")
    public ResponseData export(SyncDataSourceParam syncDataSourceParam) {
        syncDataSourceService.export(syncDataSourceParam);
        return SuccessResponseData.success();
    }

}
